package com.mdd.front.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.entity.order.OrderAccept;
import com.mdd.common.entity.order.OrderAcceptDetails;
import com.mdd.common.mapper.order.OrderAcceptDetailsMapper;
import com.mdd.common.mapper.order.OrderAcceptMapper;
import com.mdd.common.util.ImageUtils;
import com.mdd.front.JzFrontThreadLocal;
import com.mdd.front.service.order.IOrderAcceptDetailsService;
import com.mdd.front.validate.common.IdValidate;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.validate.order.OrderAcceptDetailsCreateValidate;
import com.mdd.front.validate.order.OrderAcceptDetailsSearchValidate;
import com.mdd.front.validate.order.OrderAcceptDetailsUpdateValidate;
import com.mdd.front.vo.order.OrderAcceptDetailsDetailVo;
import com.mdd.front.vo.order.OrderAcceptDetailsListedVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;

import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 接单详情信息实现类
 * @author LikeAdmin
 */
@Service
public class OrderAcceptDetailsServiceImpl implements IOrderAcceptDetailsService {
        
    @Resource
    OrderAcceptDetailsMapper orderAcceptDetailsMapper;


    @Resource
    OrderAcceptMapper orderAcceptMapper;

    /**
     * 接单详情信息列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<OrderAcceptDetailsListedVo>
     */
    @Override
    public PageResult<OrderAcceptDetailsListedVo> list(PageValidate pageValidate, OrderAcceptDetailsSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<OrderAcceptDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        orderAcceptDetailsMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:laOrderAcceptId@la_order_accept_id:str",
            "=:status:str",
            "=:remark:str",
        });

        IPage<OrderAcceptDetails> iPage = orderAcceptDetailsMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<OrderAcceptDetailsListedVo> list = new LinkedList<>();
        for(OrderAcceptDetails item : iPage.getRecords()) {
            OrderAcceptDetailsListedVo vo = new OrderAcceptDetailsListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 接单详情信息详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return OrderAcceptDetails
     */
    @Override
    public OrderAcceptDetailsDetailVo detail(Integer id) {
        OrderAcceptDetails model = orderAcceptDetailsMapper.selectOne(
                new QueryWrapper<OrderAcceptDetails>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        OrderAcceptDetailsDetailVo vo = new OrderAcceptDetailsDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 接单详情信息新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(OrderAcceptDetailsCreateValidate createValidate) {
        OrderAcceptDetails model = new OrderAcceptDetails();
        model.setLaOrderAcceptId(createValidate.getLaOrderAcceptId());
        model.setStatus(createValidate.getStatus());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        if(createValidate.getStatus()==5){
            OrderAccept accept = new OrderAccept() ;
            accept.setId(createValidate.getLaOrderAcceptId());
            accept.setUserId(JzFrontThreadLocal.getUserId());
            accept.setStatus(4);
            accept.setUpdateTime(System.currentTimeMillis() / 1000);
            Integer userId = JzFrontThreadLocal.getUserId();
            accept.setUserId(userId);
            List<String> images = createValidate.getImages();
            if(images!=null){
                accept.setImage(ImageUtils.imageListToString(images));
            }
            // 使用Lambda方式更新
            orderAcceptMapper.update(accept, new LambdaQueryWrapper<OrderAccept>()
                    .eq(OrderAccept::getId, accept.getId())
                    .eq(OrderAccept::getUserId, accept.getUserId()));
        }
        orderAcceptDetailsMapper.insert(model);
    }
    private List<String> __imageStrToList(String imageStr) {
        String[] imageList = imageStr.split(",");
        List<String> imagesLists = new LinkedList<>();
        for (String image : imageList) {
            imagesLists.add(UrlUtils.toAbsoluteUrl(image));
        }
        return imagesLists;
    }
    /**
     * 接单详情信息编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(OrderAcceptDetailsUpdateValidate updateValidate) {
        OrderAcceptDetails model = orderAcceptDetailsMapper.selectOne(
                new QueryWrapper<OrderAcceptDetails>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setLaOrderAcceptId(updateValidate.getLaOrderAcceptId());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        orderAcceptDetailsMapper.updateById(model);
    }

    /**
     * 接单详情信息删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        OrderAcceptDetails model = orderAcceptDetailsMapper.selectOne(
                new QueryWrapper<OrderAcceptDetails>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        orderAcceptDetailsMapper.delete(new QueryWrapper<OrderAcceptDetails>().eq("id", id));
    }

}
